Carbon

     

Creating and Disposing of Threads

There are two ways to create threads with calls to the Thread Manager. One is to use the NewThread function to create a single thread. The other way is to call the CreateThreadPool function to create a pool of threads that you later allocate with the NewThread function. The advantage of the latter method is that you handle memory allocation up front before fragmentation occurs.

When you create or allocate a thread with the NewThread function, you specify, among other things, the stack size. You also identify the function that is the entry point to the thread and can pass it data if you wish. You can also allocate storage that you can use to store the thread result.

When a thread finishes executing its code, the Thread Manager automatically calls the DisposeThread function to clean up after the thread. The DisposeThread function either removes the thread entirely (the default for cooperative threads) or recycles the thread into the thread pool. You can call DisposeThread yourself if you want to recycle a cooperative thread into the thread pool.

The DisposeThread function passes a parameter back to the NewThread function that initially created and launched the thread. It places the information from this parameter in the storage that the NewThread function allocated when it first created the thread. You can use this parameter to pass the thread result back to the calling thread, if you wish. For example, if you call a function to perform a calculation or process data, you can use the DisposeThread function to pass the result back.

See Passing Input and Output Parameters to a New Thread for information on how to return data from a thread to the thread that launched it.


© 2000 Apple Computer, Inc. – (Last Updated 09 May 00)